#include <iostream>
#include <vector>
#include <unordered_map>
using namespace std;

int main() {
    int p;
    cin >> p;
    while (p--)
    {
        int n, k;
        cin >> n >> k;
        int sum = 0;
        int q = 0;
        while (n--)
        {
            char str;
            cin >> str;
            if (str == 'W')
            {
                q++;
                if (q >= 3)
                {
                    sum += k;
                }
                else
                {
                    sum += 1;
                }
            }
            else
            {
                q = 0;
                sum -= 1;
            }
        }
        cout << sum << endl;
    }
    return 0;
}

class Solution {
public:
    int maxLength(vector<int>& arr) {
        // write code here
        int n = arr.size();
        unordered_map<int, int> hash;
        int left = 0;
        int right = 0;
        int k = 0;
        int my_max = 0;
        while (right < n)
        {
            hash[arr[right]]++;
            k++;
            while (hash[arr[right]] > 1)
            {
                k--;
                hash[arr[left]]--;
                left++;
            }
            if (k > my_max)
            {
                my_max = k;
            }
            right++;
        }
        return my_max;
    }
};